package com.neuedu.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

/**
 * 项目：      CrossDomain
 * 类名:       MyAttributeListener
 * 创建时间：  2020/12/17  16:51
 * 描述 :    监听域对象 属性的变化  attribute
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebListener
public class MyAttributeListener implements ServletContextAttributeListener , HttpSessionAttributeListener , ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        ServletContext app = event.getServletContext();


        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nApplication :属性添加 name: %s\t value :%s ",attributeName,value.toString()));
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nApplication : 属性 remove name: %s\t value :%s ",attributeName,value.toString()));
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        String attributeName = event.getName();
        Object oldValue = event.getValue(); //zs
        Object value = event.getServletContext().getAttribute(attributeName); //li

        System.out.println(String.format("\r\nApplication : 属性replace name: %s\t value :%s \t newValue:%s",attributeName,oldValue.toString(), value.toString()));
    }


    
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        HttpSession session = event.getSession();

        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nHttpSession: 属性添加 name: %s\t value :%s ",attributeName,value.toString()));
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nHttpSession:  属性 remove name: %s\t value :%s ",attributeName,value.toString()));
    }

    /**
     * session.setAttribute("key",value1);
     * session.setAttribute("key",value2);
     * @param event
     */
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        String attributeName = event.getName();
        Object oldValue = event.getValue(); //zs
        Object value = event.getSession().getAttribute(attributeName); //li

        System.out.println(String.format("\r\nHttpSession:  属性replace name: %s\t value :%s \t newValue:%s",attributeName,oldValue.toString(), value.toString()));
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent event) {
        ServletRequest request = event.getServletRequest();
        // ServletRequest request = event.getServletContext();

        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nServletRequest : 属性添加 name: %s\t value :%s ",attributeName,value.toString()));
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent event) {
        String attributeName = event.getName();
        Object value = event.getValue();

        System.out.println(String.format("\r\nServletRequest :  属性 remove name: %s\t value :%s ",attributeName,value.toString()));
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent event) {
        String attributeName = event.getName();
        Object oldValue = event.getValue(); //zs
        Object value = event.getServletRequest().getAttribute(attributeName); //li

        System.out.println(String.format("\r\nServletRequest :  属性replace name: %s\t value :%s \t newValue:%s",attributeName,oldValue.toString(), value.toString()));
    }
}
